﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class QuestionEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string id = Request.QueryString["id"];//编辑
                if (!string.IsNullOrEmpty(id))
                {
                    ExamQuestion examQuestion= ctx.ExamQuestions.Single(r => r.Id ==int.Parse(id));
                    QuestionType.Value = examQuestion.QuestionType;
                    Question.Value = examQuestion.Question;
                    Options.Value = examQuestion.Options;
                    Answer.Value = examQuestion.Answer;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string typeId = Request.QueryString["typeId"];//新增
            string id = Request.QueryString["id"];//编辑
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ExamQuestion examQuestion;
            if (string.IsNullOrEmpty(id))
            {
                examQuestion = new ExamQuestion();
                examQuestion.TypeId =int.Parse(typeId);
                ctx.ExamQuestions.InsertOnSubmit(examQuestion);
            }
            else
            {
                examQuestion = ctx.ExamQuestions.Single(r => r.Id ==int.Parse(id));
            }
            examQuestion.QuestionType = QuestionType.Value;
            examQuestion.Question = Question.Value;
            examQuestion.Options = Options.Value;
            examQuestion.Answer = Answer.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}